 
package com.leyu.mall.site.shopcar.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leyu.mall.domain.goods.GoodsBase;
import com.leyu.mall.service.goods.IGoodsBaseService;
import com.leyu.mall.site.shopcar.car.ShopCar;

 
@Controller
@Scope(value = "prototype")
@RequestMapping("shopcar")
//@SessionAttributes("shopcar") 
public class ShopCarController {
	protected Log log = LogFactory.getLog(this.getClass());
	 
	@Resource(name = "goodsBaseServiceImpl")
	private IGoodsBaseService goodsBaseServiceImpl;
	
	/**
	 * HttpServletRequest req,
	 * @param model
	 * @param goodsID
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "{goodsID}", method = RequestMethod.GET)
	public String shopcar(Model model, @PathVariable Long goodsID,HttpServletRequest request) {
		try {
			
			ShopCar shopcar = (ShopCar) request.getSession().getAttribute("shopcar");
			
			System.out.println("-------shopcar-goodsID-----------------------"+goodsID); 
			
			if (shopcar == null) {
				// ���ﳵΪ��
				shopcar = new ShopCar();
				GoodsBase mg = this.goodsBaseServiceImpl.getGoodsBaseById(goodsID);
				mg.setGoodsAmount(1);
				shopcar.addGoods(mg);
			} else {
				// ��ֹˢҳ�����ӹ��ﳵ�е���Ʒ����
				boolean contain = shopcar.getCars().containsKey(goodsID);
				if (!contain) {
					// ���ﳵ��û�������Ʒ��
					GoodsBase mg = this.goodsBaseServiceImpl.getGoodsBaseById(goodsID);
					mg.setGoodsAmount(1);
					shopcar.addGoods(mg);
				}
			}
			 model.addAttribute("shopcar",shopcar); //����ModelMap�����һ������
			 
			model.addAttribute("shopcarlist", shopcar.getCars());

//			request.getSession().setAttribute("carcount", shopcar.getCars().size());
//			request.getSession().setAttribute("allPrice",
//					shopcar.getGoodsAllPrice());
			request.getSession().setAttribute("shopcar", shopcar);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "shopcar/shopcar.html";
	}
	
//
//	@RequestMapping("/removegoods.html")
//	 
//	@RequestMapping("/addcar.html")
//	 
//	@RequestMapping("/clearcar.html")
// 
//	@RequestMapping("/shopcar.html")
//	 
//	@RequestMapping("/addamount.html")
// 
//
//	@RequestMapping("/removeamount.html")
//	 
//	@RequestMapping("/settle.htm")
//	 
//	@RequestMapping(value = "/settle_pay.htm")
//	 
//	@RequestMapping("/ordercommit.htm")
//	 
//	@RequestMapping("/onlineorder.htm")
	 
}
